package com.cssy.yd.logic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.basic.entity.BasicStorageinfoDO;
import com.cssy.yd.basic.service.IBasicStorageService;
import com.cssy.yd.logic.dao.LogicWhmoveOutwhDAO;
import com.cssy.yd.logic.dao.LogicWhmoveOutwhItemDAO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.entity.LogicWhmoveOutwhDO;
import com.cssy.yd.logic.entity.LogicWhmoveOutwhItemDO;
import com.cssy.yd.logic.service.ILogicStockTransferStartService;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicStockTransferStartService")
public class LogicStockTransferStartServiceImpl implements ILogicStockTransferStartService{

	@Resource(name = "logicWhmoveOutwhDAO")
	private LogicWhmoveOutwhDAO logicWhmoveOutwhDAO;
	
	@Resource(name = "logicWhmoveOutwhItemDAO")
	private LogicWhmoveOutwhItemDAO logicWhmoveOutwhItemDAO;
	
	@Resource(name = "logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name="basicwhService")	
	private IBasicStorageService basicwhService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhmoveOutwhDO>  list= logicWhmoveOutwhDAO.find(scs);
		rs.setRecordCount(logicWhmoveOutwhDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		LogicWhmoveOutwhDO basic=new LogicWhmoveOutwhDO();
		basic.setDutime(new Date());
		basic.setDuuser(user.getName());
		basic.setId(id);
		basic.setRemoved(1);
		logicWhmoveOutwhItemDAO.deleteLogicWhmoveOutwhItemDOByPrimaryKey(id);
		logicWhmoveOutwhDAO.updateLogicWhmoveOutwhDO(basic);
		return null;
	}

	@Override
	public Integer update(LogicWhmoveOutwhDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicWhmoveOutwhDAO.updateLogicWhmoveOutwhDO(t);
	}

	@Override
	public LogicWhmoveOutwhDO save(LogicWhmoveOutwhDO t, User user)
			throws SyBaseException {
		Date date=new Date();
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		t.setDcruser(user.getName());
		t.setDctime(date);
		logicWhmoveOutwhDAO.insertLogicWhmoveOutwhDO(t);
		return t;
	}

	@Override
	public LogicWhmoveOutwhDO findListById(String id) throws SyBaseException {
		return logicWhmoveOutwhDAO.findLogicWhmoveOutwhDOByPrimaryKey(id);
	}

	@Override
	public Integer update(LogicWhmoveOutwhDO curAP, List<LogicWhmoveOutwhItemDO> listLWOID,
			User user) throws SyBaseException {
		logicWhmoveOutwhItemDAO.deleteByLogicWhmoveOutwhDOId(curAP.getId());
		
		BasicStorageinfoDO outWhInfo = basicwhService.findByWhCode(curAP.getOutwhcode()); //获取出库仓信息
		BasicStorageinfoDO inWhInfo = basicwhService.findByWhCode(curAP.getInwhcode());	//获取入库仓信息	
		
		curAP.setOutwhname(outWhInfo.getName());
		curAP.setInwhname(inWhInfo.getName());
		
		for (LogicWhmoveOutwhItemDO lwoid : listLWOID){
			String id = IDCodeTypeUtil.getId();
			lwoid.setId(id);
			lwoid.setOtheroutwhid(curAP.getId());
			logicWhmoveOutwhItemDAO.insertLogicWhmoveOutwhItemDO(lwoid);
			
			if (curAP.getBillstatus() == 2){
				
				
				boolean isAmple = logicWhAffStockService.validateStorageQuaitity(lwoid.getCode(), curAP.getOutwhcode(), 
						null, lwoid.getStatus(), null, lwoid.getQuantity());
				if(!isAmple){
					throw new SyBaseException("错误:编码为" + lwoid.getCode() + "的物品在指定仓库仓位处库存不足!!");
				}
			}
		}
		
		return this.update(curAP, user);
	}

	@Override
	public LogicWhmoveOutwhDO save(LogicWhmoveOutwhDO curAP,
			List<LogicWhmoveOutwhItemDO> listpi, User user) throws SyBaseException {
		
		BasicStorageinfoDO outWhInfo = basicwhService.findByWhCode(curAP.getOutwhcode()); //获取出库仓信息
		BasicStorageinfoDO inWhInfo = basicwhService.findByWhCode(curAP.getInwhcode());	//获取入库仓信息	
		
		curAP.setOutwhname(outWhInfo.getName());
		curAP.setInwhname(inWhInfo.getName());
		
		for (LogicWhmoveOutwhItemDO lii : listpi){
			String id = IDCodeTypeUtil.getId();
			lii.setId(id);
			lii.setOtheroutwhid(curAP.getId());
			logicWhmoveOutwhItemDAO.insertLogicWhmoveOutwhItemDO(lii);
		}
		return this.save(curAP, user);
	}

	@Override
	public void startsave(LogicWhmoveOutwhDO curAP,
			HttpServletRequest request) throws SyBaseException {
		try{//新增草稿或者草稿提交时操作
			String addressInfos =request.getParameter("addressInfos");
			String zkrq=request.getParameter("zkrq");//转库日期
			List<LogicWhmoveOutwhItemDO> axxlist=JSONArray.parseArray(addressInfos, LogicWhmoveOutwhItemDO.class);
			if (curAP.getId() != null && !"".equals(curAP.getId())) {// 提交
				curAP.setDuuser(SessionUtil.getUser(request).getName());
				curAP.setDutime(new Date());
				int rst=0;
				curAP.setZkrq(DateUtils.stringToDate(zkrq));
				rst=logicWhmoveOutwhDAO.updateLogicWhmoveOutwhDO(curAP);
				if(rst==1){
						logicWhmoveOutwhItemDAO.deleteByLogicWhmoveOutwhDOId(curAP.getId());
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setOtheroutwhid(curAP.getId());
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							logicWhmoveOutwhItemDAO.insertLogicWhmoveOutwhItemDO(axxlist.get(i));
						}
				}else{
					throw new SyBaseException("您修改的当前行数据已发生改变,请重新点开查看");
				}
			} else {// 新增
				String id=IDCodeTypeUtil.getId();
				curAP.setOtheroutwhcode(syCodeService.getCode("ST","ST",SyCode.DATE_YYMM,5));
				curAP.setId(id);
				curAP.setIntype(Short.parseShort("0"));
				curAP.setDcruser(SessionUtil.getUser(request).getName());
				curAP.setDctime(new Date());
				curAP.setZkrq(DateUtils.stringToDate(zkrq));
				logicWhmoveOutwhDAO.insertLogicWhmoveOutwhDO(curAP);
				for(int i=0;i<axxlist.size();i++){
					axxlist.get(i).setOtheroutwhid(id);
					axxlist.get(i).setId(IDCodeTypeUtil.getId());
					logicWhmoveOutwhItemDAO.insertLogicWhmoveOutwhItemDO(axxlist.get(i));
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public void startCheck(LogicWhmoveOutwhDO curAP, HttpServletRequest request)
			throws SyBaseException {
		try{
			LogicWhmoveOutwhDO lpwd = this.findListById(curAP.getId());
			List<LogicWhmoveOutwhItemDO> lwlist=new ArrayList<LogicWhmoveOutwhItemDO>();
			int rst=0;
			if(curAP.getBillstatus()==2){  //二级审核
				if(lpwd.getBillstatus()==1){
					lpwd.setVersion(curAP.getVersion());
					lpwd.setBillstatus(curAP.getBillstatus());
					this.update(lpwd, SessionUtil.getUser(request));
				}else{
					throw new SyBaseException("当前单据未在您审核权限内");
				}
				
			}
			if(curAP.getBillstatus()==3){  //三级审核
				if(lpwd.getBillstatus()==1||lpwd.getBillstatus()==2){
					lpwd.setVersion(curAP.getVersion());
					lpwd.setBillstatus(curAP.getBillstatus());
					rst=this.update(lpwd, SessionUtil.getUser(request));
					if(1==rst){
						lwlist=logicWhmoveOutwhItemDAO.findListByOutwhId(lpwd.getId());
						if(lwlist.size()>0){
							for(int i=0;i<lwlist.size();i++){
								LogicWhAffDO lwhaff=new LogicWhAffDO();
								lwhaff.setBillCode(lpwd.getOtheroutwhcode());
								lwhaff.setBillType("ST");
								lwhaff.setWhCode(lpwd.getOutwhcode());
								lwhaff.setWhname(lpwd.getOutwhname());
								lwhaff.setCode(lwlist.get(i).getCode());
								lwhaff.setName(lwlist.get(i).getName());
								lwhaff.setSpec(lwlist.get(i).getSpec());
								lwhaff.setBatch(lwlist.get(i).getBatch());
					    		lwhaff.setBrand(lwlist.get(i).getBrand());
								lwhaff.setStatus(lwlist.get(i).getStatus());
								lwhaff.setDcruser(SessionUtil.getUser(request).getName());
								lwhaff.setDctime(new Date());
					//			lwhaff.setHubCode(lwlist.get(i).getHubcode());
					//			lwhaff.setHubName(lwlist.get(i).getHubname());
								lwhaff.setUnit(lwlist.get(i).getUnit());
								lwhaff.setQuantity(-lwlist.get(i).getQuantity());
								lwhaff.setJunit(lwlist.get(i).getJunit());
								lwhaff.setJquantity(-lwlist.get(i).getJquantity());
								lwhaff.setBzq(lwlist.get(i).getBzq());
								lwhaff.setYxq(lwlist.get(i).getYxq());
								lwhaff.setScrq(lwlist.get(i).getScrq());
								//可用库存减少
								if (lwlist.get(i).getQuantity() != null && !"".equals(lwlist.get(i).getQuantity()) && lwlist.get(i).getQuantity() > 0 )
									logicWhAffStockService.updateWhAffstockQuantity(lwhaff, SessionUtil.getUser(request),false);
								
								LogicWhAffDO lwhaff1=new LogicWhAffDO();
								lwhaff1.setBillCode(lpwd.getOtheroutwhcode());
								lwhaff1.setBillType("ST");
								lwhaff1.setWhname(lpwd.getOutwhname());
								lwhaff1.setWhCode(lpwd.getOutwhcode());
								lwhaff1.setUnit(lwlist.get(i).getUnit());
								lwhaff1.setCode(lwlist.get(i).getCode());
								lwhaff1.setName(lwlist.get(i).getName());
								lwhaff1.setSpec(lwlist.get(i).getSpec());
								lwhaff1.setBrand(lwlist.get(i).getBrand());
								lwhaff1.setStatus("5");
								lwhaff1.setQuantity(lwlist.get(i).getQuantity());
								lwhaff1.setBatch(lwlist.get(i).getBatch());
								lwhaff1.setDcruser(SessionUtil.getUser(request).getName());
								lwhaff1.setDctime(new Date());
						//		lwhaff.setHubCode(lwlist.get(i).getHubcode());
						//		lwhaff1.setHubName(lwlist.get(i).getHubname());
								lwhaff1.setJunit(lwlist.get(i).getJunit());
								lwhaff1.setJquantity(lwlist.get(i).getJquantity());
								lwhaff1.setBzq(lwlist.get(i).getBzq());
								lwhaff1.setYxq(lwlist.get(i).getYxq());
								lwhaff1.setScrq(lwlist.get(i).getScrq());
								//添加在途库存
								if (lwlist.get(i).getQuantity() != null && !"".equals(lwlist.get(i).getQuantity()) && lwlist.get(i).getQuantity() > 0 )
									logicWhAffStockService.updateWhAffstockQuantity(lwhaff1, SessionUtil.getUser(request),false);
							}
						}
					}else{
						throw new SyBaseException("您修改的当前行数据已发生改变,请重新点开查看");
					}
				}else{
					throw new SyBaseException("当前单据未在您审核权限内");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	
}
